#include <gtk/gtk.h>
#include<string.h>
#include "update.h"
#include "newinfo.h"
#include "file.h"
#include "clist.h"
#include "message.h"

void
on_button_update_info_help_clicked(GtkButton *button,gpointer  user_data)
{
	GtkWidget *window=(GtkWidget *)user_data;
	gchar msg[]="在修改后的信息中，姓名不能为空，邮箱格式必须正确!";
	message_box_for_new_info(window,msg);
}

void
on_button_update_info_cancel_clicked(GtkButton *button,gpointer user_data)
{
	GtkWidget *window=(GtkWidget *)user_data;
	gtk_widget_destroy(window);
}

void
on_button_update_info_ok_clicked (GtkButton *button,gpointer  user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	struct file_ctrl *global_file=newinfo->global_file;
        gint i,j,len;
	gchar *info[MAX_COLUMNS];
	gchar info_nums[10];

	#ifdef _DEBUG_
		printf("on_button_update_info_ok_clicked:start---\n");
	#endif
	info[0]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_name));
	if(strlen(info[0])==0) {
		on_button_update_info_help_clicked(NULL,newinfo->window);
		return;
	}
	info[1]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_class));
	info[2]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_tel));
	info[3]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_mail));
	if(strlen(info[3])!=0 && strcmp(info[3],"---")!=0 && strchr(info[3],'@')==NULL) {
		gchar msg[]="在修改的信息中，邮箱格式不正确!";
		message_box_for_new_info(newinfo->window,msg);
		return ;
	}
	info[4]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_blog));
	
	for(i=0;i<global_file->cur_file->col_nums;i++) {
		len=strlen(info[i]);
		for(j=0;j<len;j++) {
			if(info[i][j]==' ')
				info[i][j]='-';
		}
		if(strlen(info[i])==0) {
			info[i]="---";
		} 
	}
	gtk_clist_remove(GTK_CLIST(newinfo->clist->clist_show),
							newinfo->clist->show_selected_row);
	gtk_clist_insert(GTK_CLIST(newinfo->clist->clist_show),
							newinfo->clist->show_selected_row,(gchar **)info);
	
	/*set modify flags with TRUE to mark current data file has been modified*/
	global_file->cur_file->modify=TRUE;
	global_file->data_mod=TRUE;
	save_tmp_file(newinfo);

	#ifdef _DEBUG_
		printf("global_cur_file:info_nums=%d,%d\n",global_file->cur_file->info_nums,
							*newinfo->clist->show_all_row);
	#endif
	gtk_widget_destroy(newinfo->window);
}

void
on_window_update_info_destroy(GtkObject *object,gpointer user_data)
{
	gtk_widget_destroy(GTK_WIDGET(object));
}


void
on_button_update_group_help_clicked(GtkButton *button,gpointer  user_data)
{
	GtkWidget *window=(GtkWidget *)user_data;
	gchar msg[]="请输入一个分组的名称,组名不能重复！";
	message_box_for_add_group(window,msg);
}

void
on_button_update_group_cancel_clicked(GtkButton *button,gpointer user_data)
{
	GtkWidget *window=(GtkWidget *)user_data;
	gtk_widget_destroy(window);
}

void
on_button_update_group_ok_clicked (GtkButton *button,gpointer  user_data)
{
	struct addgroup_struct *addgroup=(struct addgroup_struct *)user_data;
	struct file_ctrl *global_file=addgroup->global_file;
	struct file_list *last;
	gchar *info[2];
	gchar name[2][80];
	gint i,len;
	#ifdef _DEBUG_
		printf("on_button_update_group_ok_clicked :---\n");
	#endif
	info[0]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(addgroup->entry_name));
	for(i=0,len=strlen(info[0]);i<len;i++) {
		if(info[0][i]==' ')
			info[0][i]='-';
	}
	if(len==0) {
		on_button_update_group_help_clicked (NULL,addgroup->window);
		return;
	}
	if(len>=MAX_CLIST_NAME) {
		gchar msg[]="输入的组名不能大于30个字符!";
		message_box_for_add_group(addgroup->window,msg);
		return;
	}
	if(strcmp(info[0],global_file->current)==0)
		return;
	info[1]="0";
	for(last=global_file->files;last->next!=NULL;last=last->next) {
		if(strcmp(info[0],last->name)==0||strcmp(info[0],last->newname)==0) {
			gchar msg[]="输入的组名已经存在!";
			message_box_for_add_group(addgroup->window,msg);
			return;
		}
	}
	
	strncpy(global_file->current,info[0],MAX_CLIST_NAME);
	strncpy(global_file->cur_file->newname,info[0],MAX_CLIST_NAME);
	g_snprintf(name[0],80,"%s/%s",global_file->data,global_file->cur_file->name);
	g_snprintf(name[1],80,"%s/%s",global_file->data,global_file->cur_file->newname);
	rename(name[0],name[1]);
	
	g_snprintf(name[0],80,"%s/%s",global_file->tmp,global_file->cur_file->name);
	g_snprintf(name[1],80,"%s/%s",global_file->tmp,global_file->cur_file->newname);
	rename(name[0],name[1]);
	gtk_clist_set_text(GTK_CLIST(addgroup->clist->clist_group),
							addgroup->clist->group_selected_row,0,info[0]);
	global_file->modify=TRUE;
	save_config_file(global_file);
	gtk_widget_destroy(addgroup->window);
}

void
on_window_update_group_destroy(GtkObject *object,gpointer user_data)
{
	gtk_widget_destroy(GTK_WIDGET(object));
}
